//
//  ProblemViewController.m
//  MagazineShangejie
//
//  Created by wen on 15/9/21.
//  Copyright © 2015年 CnbizMedia. All rights reserved.
//

#import "ProblemViewController.h"
#import "Util.h"
#import "OpinionViewController.h"
@interface ProblemViewController (){

NSDictionary *wordDic;
NSMutableArray *titleArray;
NSMutableArray *wordArray;
    UITableViewCell *cell;
    
}

@property (weak, nonatomic) IBOutlet UITableView *complainTableView;

@end

@implementation ProblemViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.hidesBottomBarWhenPushed = YES;

    _complainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self setRightBarButton];
    self.title = @"问题反馈";
    [[AppAPIClient sharedClient] GET:@"Feedback/List" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"Feedback/List operation = =%@",operation);
//        shareURL = operation.request.URL;
//        NSLog(@"shareURL -- %@",shareURL);
        
        NSMutableDictionary *result = responseObject[@"data"];
        NSLog(@"luntan-data == %@",result);
        if([result isKindOfClass:[NSNull class]] || [result count] == 0){
            return;
        }
        if([result isKindOfClass:[NSNull class]] || [result count] == 0||[result[@"content"] count]==0){
            return;
        }
        
//        wordArray = [[NSMutableArray alloc] initWithObjects:result[@"content"] ,nil];
        wordArray = [[NSMutableArray alloc] initWithArray:result[@"content"]  copyItems:YES];
        NSLog(@"wordArray -- %@",wordArray);
        
        
        [_complainTableView reloadData];
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
    
    UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
    //    [button setImage:[UIImage imageNamed:@"share01"] forState:UIControlStateNormal];
    [button setFrame:CGRectMake(10, 5, 80, 35)];
    [button setTitle:@"我要吐槽" forState:UIControlStateNormal];
    button.titleLabel.font    = [UIFont systemFontOfSize: 15];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightBarButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem*rightItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    
    self.navigationItem.rightBarButtonItem= rightItem;

    



    UIButton *buttonf=[UIButton buttonWithType:UIButtonTypeCustom];
    [buttonf setFrame:CGRectMake(0, 0, 22, 22)];
    [buttonf addTarget:self action:@selector(popOfDismss) forControlEvents:UIControlEventTouchUpInside];
    [buttonf setImage:[UIImage imageNamed:@"dismiss"] forState:UIControlStateNormal];
    
    NSString *newOldYear = [UserDefaults objectForKey:@"NewYear" ];
    if ([newOldYear isEqualToString:@"NewYear"]) {
        //状态栏的文字颜色换成白色
        //        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        
        //            [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        [buttonf setImage:[UIImage imageNamed:@"dismissGray"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
  
    UIBarButtonItem*leftBarItem = [[UIBarButtonItem alloc]initWithCustomView:buttonf];
    self.navigationItem.leftBarButtonItem=leftBarItem;

}


-(void)popOfDismss{
    if ([[self.navigationController viewControllers] count]>=2) {
        [self.navigationController popViewControllerAnimated:YES];
        
    }else{
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}
- (void)setRightBarButton{

   }

- (void)rightBarButtonClicked{
    UIStoryboard*story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
            OpinionViewController*opin=[story instantiateViewControllerWithIdentifier:@"OpinionViewController"];
            opin.view.backgroundColor=[UIColor colorWithWhite:0.890 alpha:1.000];
            [opin opinionInWWW:1];
            [self.navigationController pushViewController:opin animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





#pragma mark - Table view data source
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    //    if (section==0) {
    //        return [self tableHeader];
    //    }else{
    return nil;
    //    }
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    //#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    return wordArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"wordCell" forIndexPath:indexPath];
    ////    = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    //
    
    NSDictionary *replyDic = wordArray[indexPath.row];
    NSString *replyString = [replyDic objectForKey:@"reply"];
    NSLog(@"replyString == %@",replyString);
    if (replyString.length >1){
        return 170;
    }
    
    else if (replyString.length == 0)
        
    {
        return 75;
    }
    
    return 0;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    NSDictionary *replyDic = wordArray[indexPath.row];
    NSString *replyString = [replyDic objectForKey:@"reply"];
    
    //有回复
    if (replyString.length >1){
        
        cell = [tableView dequeueReusableCellWithIdentifier:@"questionAnswerCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"questionAnswerCell"];
            
        }
        
        UILabel*label1=(UILabel*)[cell.contentView viewWithTag:11];
        label1.text = @"问题";
        label1.text =  [replyDic objectForKey:@"introduce"];
        label1.numberOfLines = 0;
        [label1 sizeToFit];
        
        
        UILabel*label2=(UILabel*)[cell.contentView viewWithTag:22];
        label2.text = @"吐槽时间";
        UILabel*label6 = (UILabel*)[cell.contentView viewWithTag:66];
        label2.text = @"官方吐槽时间";

        NSString *timeString = [replyDic objectForKey:@"addtime"];
        NSLog(@"timeString -- %@",timeString);

        //得到运动时间，需要转成年月日
        NSString *formatString = @"YYYY-MM-dd";
        
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:formatString];
        NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeString intValue] - 28800];
        NSString *dateString =[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:confromTimesp] ];
        NSLog(@"date: %@", dateString);
        label2.text = dateString;
        label6.text = [NSString stringWithFormat:@"官方回复:%@", dateString ];
        
//        UILabel*label3 = (UILabel*)[cell.contentView viewWithTag:33];
//        label3.text = @"已回复";

//        UIView *backView = (UIView *)[cell.contentView viewWithTag:44];
//        backView.backgroundColor = [UIColor lightGrayColor];
        
        UILabel *answer = (UILabel *)[cell.contentView viewWithTag:55];
        answer.text = replyString;
        
        
        cell.userInteractionEnabled = NO;
        
        
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        return cell;
    }
    
    
    //无回复
    else if (replyString.length == 0)
        
    {
        cell = [tableView dequeueReusableCellWithIdentifier:@"questionNoAnswerCell" forIndexPath:indexPath];
        
        if (cell==nil) {
            
            cell=[tableView dequeueReusableCellWithIdentifier:@"questionNoAnswerCell"];
            
        }
        
        UILabel*label1=(UILabel*)[cell.contentView viewWithTag:11];
        label1.text = @"问题";
        label1.text =  [replyDic objectForKey:@"introduce"];
        label1.numberOfLines = 0;
        [label1 sizeToFit];
        
        
        UILabel*label2=(UILabel*)[cell.contentView viewWithTag:22];
        label2.text = @"吐槽时间";
        UILabel*label6 = (UILabel*)[cell.contentView viewWithTag:66];
        label2.text = @"官方吐槽时间";
        
        NSString *timeString = [replyDic objectForKey:@"addtime"];
        NSLog(@"timeString -- %@",timeString);
        
        //得到运动时间，需要转成年月日
        NSString *formatString = @"YYYY-MM-dd";
        
        NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
        [dateFormat setDateFormat:formatString];
        NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:(NSTimeInterval)[timeString intValue] - 28800];
        NSString *dateString =[NSString stringWithFormat:@"%@",[dateFormat stringFromDate:confromTimesp] ];
        NSLog(@"date: %@", dateString);
        label2.text = dateString;
        label6.text = dateString;
        
//        UILabel*label3 = (UILabel*)[cell.contentView viewWithTag:33];
//        label3.text = @"未回复";

        cell.userInteractionEnabled = NO;
        
        
        
        cell.backgroundColor = UIColorFromRGB(0xf5f5f5);
        return cell;
    }
    
    
    
    
    
    return nil;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //    indexPath.row;
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//点击后不会有阴影
    
    //    NSDictionary *lunTanDic = userData[indexPath.row];
    //    NSLog(@"lunTanDic == %@",lunTanDic);
    //
    //    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    //    [userDefault setObject:lunTanDic forKey:@"lunTanDic"];
    //    [userDefault synchronize];//存入文件
    //
    //
    //    if ([self.title isEqualToString:@"论坛"]) {
    //        [self performSegueWithIdentifier:@"IWanIdentifier" sender:indexPath];
    //    }
    
    
}


@end
